package cn.sccl.dirms.dev.controller;

import cn.sccl.common.model.PageResult;
import cn.sccl.common.util.SecurityUtils;
import cn.sccl.common.web.BaseController;
import cn.sccl.dirms.security.model.UserPrincipal;
import com.alibaba.fastjson.JSON;
import org.springframework.beans.factory.annotation.Autowired;
import com.fasterxml.jackson.annotation.JsonView;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import cn.sccl.dirms.dev.model.Graphs;
import cn.sccl.dirms.dev.service.GraphsManager;

/**
 * 图形集信息 的Controller
 * Created by liupan on 2017-06-25.
 */
@RestController
@RequestMapping(value = "/dirms/dev/graphs")
public class GraphsController extends BaseController {

    @Autowired
    private GraphsManager graphsManager;

    /**
     * 显示所有数据
     */
    @RequestMapping(value = "/all", method = RequestMethod.GET)
    public List<Graphs> listAllGraphs() {
        return graphsManager.listAll();
    }


    /**
     * 根据编号获取图形集信息
     *
     * @param grsNo
     * @return
     */
    @RequestMapping(value = "/getByNo/{grsNo}", method = RequestMethod.GET)
    public Graphs getGraphByNo(@PathVariable String grsNo) {
        Map<String, Object> find = new HashMap<>();
        find.put("grsNo", grsNo);
        return graphsManager.findUniqueBy(find);
    }

    /**
     * 根据图表类型获取图形集信息
     *
     * @param grsType
     * @return
     */
    @RequestMapping(value = "/listGraphByType", method = RequestMethod.GET)
    public List<Graphs> listGraphByType(@RequestParam String grsType) {
        Map<String, Object> find = new HashMap<>();

        if(grsType!=null&&!"".equals(grsType)){
            find.put("grsType", grsType);
        }
        return graphsManager.findBy(find);

    }
    /**
     * 分页查询，分页参数放置在url路径中
     *
     * @param pageIndex 页码
     * @param pageSize  分页大小
     * @param request
     * @return
     */
    @RequestMapping(value = "/page/{pageIndex}/{pageSize}", method = RequestMethod.GET)
    public PageResult<Graphs> pageQueryGraphs(@PathVariable("pageIndex") long pageIndex, @PathVariable("pageSize") int pageSize, HttpServletRequest request) {
        return graphsManager.pageQueryByLike(pageIndex, pageSize, getQueryData(request));
    }

    /**
     * 分页查询，分页参数放置在request中

     *
     * @param request
     * @return
     */
    @RequestMapping(value = "/page", method = RequestMethod.GET)
    public PageResult<Graphs> pageQueryGraphs(HttpServletRequest request) {
        return graphsManager.pageQueryByLike(getPageIndex(request), getPageSize(request), getQueryData(request));
    }

    /**
     * 保存数据
     *
     * @param graphs
     * @return
     */
    @RequestMapping(value = "/save", method = RequestMethod.POST)
    public Graphs saveGraphs(@RequestBody Graphs graphs) {
        graphsManager.save(graphs);
        return graphs;
    }

    /**
     * 发布监控图形到设备监控菜单
     * @param graphs 图形的数据
     * @param displayPath 菜单指向的显示地址
     * @param parentMenuName 父级菜单名称
     */
    @RequestMapping(value = "/sendToMenu/{displayPath}/{parentMenuName}", method = RequestMethod.POST)
    public void sendToMenu(@RequestBody Graphs graphs,@PathVariable String displayPath,@PathVariable String parentMenuName,HttpServletRequest request) {
//        Graphs graphs = JSON.parseObject(graphsStr,Graphs.class);
        try {
            displayPath = URLDecoder.decode(displayPath,"utf-8");
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
            displayPath = "";
        }
        try {
            parentMenuName = URLDecoder.decode(parentMenuName,"utf-8");
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
            parentMenuName = "";
        }
        if("".equalsIgnoreCase(displayPath) || "".equalsIgnoreCase(parentMenuName)){
            return;
        }

        UserPrincipal u = SecurityUtils.getUserPrincipal(request.getSession());
        graphsManager.sendToMenu(graphs,displayPath,parentMenuName,u==null?"":u.getId());
    }

    /**
     * 根据ID删除数据
     *
     * @param id
     */
    @RequestMapping(value = "/{id}", method = RequestMethod.DELETE)
    @JsonView(Object.class)
    public void deleteGraphs(@PathVariable("id") String id) {
        graphsManager.delete(id);
    }
}
